/*
 * Copyright (c) 2009-2012 Xilinx, Inc.  All rights reserved.
 *
 * Xilinx, Inc.
 * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
 * COURTESY TO YOU.  BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
 * ONE POSSIBLE   IMPLEMENTATION OF THIS FEATURE, APPLICATION OR
 * STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION
 * IS FREE FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE
 * FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
 * XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
 * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO
 * ANY WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE
 * FROM CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

/*
 * helloworld.c: simple test application
 *
 * This application configures UART 16550 to baud rate 9600.
 * PS7 UART (Zynq) is not initialized by this application, since
 * bootrom/bsp configures it to baud rate 115200
 *
 * ------------------------------------------------
 * | UART TYPE   BAUD RATE                        |
 * ------------------------------------------------
 *   uartns550   9600
 *   uartlite    Configurable only in HW design
 *   ps7_uart    115200 (configured by bootrom/bsp)
 */

#include "platform.h"
#include "xparameters.h"

#include "regs.h"
#include "device.h"
#include "widget.h"
#include "menu.h"
#include "stdio.h"
#include "xstatus.h"
#include "xintc.h"
#include "xil_exception.h"
#include "xenv_standalone.h"

/************************** Constant Definitions ****************************/
unsigned int* fusion_base  = (unsigned int*)XPAR_FUSION_CTRL_0_S_AXI_RNG00_BASEADDR;
/*
 * The following constant is used to wait after an LED is turned on to make
 * sure that it is visible to the human eye.  This constant might need to be
 * tuned for faster or slower processor speeds.
 */
#define  RESERVE_STAR      0xaf800000

unsigned int* menu_fb0     = (unsigned int*)(RESERVE_STAR + 0x80000*0x0);
unsigned int* menu_fb1     = (unsigned int*)(RESERVE_STAR + 0x80000*0x1);
unsigned int* menu_fb2     = (unsigned int*)(RESERVE_STAR + 0x80000*0x2);
unsigned int* disp_fb0     = (unsigned int*)(RESERVE_STAR + 0x80000*0x3);
unsigned int* disp_fb1     = (unsigned int*)(RESERVE_STAR + 0x80000*0x4);
unsigned int* disp_fb2     = (unsigned int*)(RESERVE_STAR + 0x80000*0x5);
unsigned int* disp_fb3     = (unsigned int*)(RESERVE_STAR + 0x80000*0x6);
unsigned int* disp_fb4     = (unsigned int*)(RESERVE_STAR + 0x80000*0x7);
unsigned int* disp_fb5     = (unsigned int*)(RESERVE_STAR + 0x80000*0x8);

extern UIState uistate;
extern unsigned int * mask_fb;

void intr_hdlr(void *InstancePtr) {
    uistate.keyentered = rKEY_VALUE;
    uistate.keypressed = 1;
}

int main()
{
	int nI;
	static XIntc intc;
    init_platform();

    mask_fb = menu_fb0;
    xil_printf("******************stag0 cpu init***********************************\n\r");
    XIntc_Initialize(&intc, XPAR_AXI_INTC_0_DEVICE_ID);
    XIntc_Connect(&intc, XPAR_AXI_INTC_0_SYSTEM_TP_INTR_0_INTR, (XInterruptHandler) intr_hdlr, (void *) 0 );
    XIntc_Enable(&intc, XPAR_AXI_INTC_0_SYSTEM_TP_INTR_0_INTR);
    XIntc_Start(&intc, XIN_REAL_MODE);
    microblaze_enable_interrupts();
    xil_printf("******************stag1 mem init***********************************\n\r");
	rMENU_BUF0 = (unsigned int)menu_fb0;
    rMENU_BUF1 = (unsigned int)menu_fb1;
    rMENU_BUF2 = (unsigned int)menu_fb2;

    rCAM0_BUF0 = (unsigned int)disp_fb0;
    rCAM0_BUF1 = (unsigned int)disp_fb1;
    rCAM0_BUF2 = (unsigned int)disp_fb2;

    rCAM1_BUF0 = (unsigned int)disp_fb3;
    rCAM1_BUF1 = (unsigned int)disp_fb4;
    rCAM1_BUF2 = (unsigned int)disp_fb5;

    for(nI=0;nI<(PIX_CNT);nI ++) *(menu_fb0 + nI) = COLOR_YELLOW;
    //for(nI=0;nI<(PIX_CNT);nI ++) *(disp_fb0 + nI) = COLOR_GREEN;
    //for(nI=0;nI<(PIX_CNT);nI ++) *(disp_fb1 + nI) = COLOR_BLUE;
    //for(nI=0;nI<(PIX_CNT);nI ++) *(disp_fb2 + nI) = COLOR_RED;
    //for(nI=0;nI<(PIX_CNT);nI ++) *(disp_fb3 + nI) = COLOR_BLUE;
    //for(nI=0;nI<(PIX_CNT);nI ++) *(disp_fb4 + nI) = COLOR_RED;
    //for(nI=0;nI<(PIX_CNT);nI ++) *(disp_fb5 + nI) = COLOR_GREEN;
    xil_printf("******************stag2 disp init*************************************\n\r");
    rVGA_PCNT  = PIX_CNT; // vga_pix_size
    rVGA_CTRL  = (0 << 8)|(0x1<<2)|(0x1<<1)|0x1; // masksel[15:8] start lcd disp[0], Menu enable[1],bar hide[2]
    rBACKLIGHT = 10;
    rBLACK_PIX = 0x10;
	xil_printf("******************stag3 camera init***********************************\n\r");
    rCAM0_CTRL = (0x80<<24|0x80 << 16); // start video
	rCAM1_CTRL = (0x80<<24|0x80 << 16); // start video
	rCAM0_PWR  = 0x101; //start cfg
	rCAM1_PWR  = 0x101; //start cfg
   
    rVIEW_XY   = 0x0;
    xil_printf("******************stag3 aux init**************************************\n\r");
    rHEAT_CTRL = (0x0 << 31|70 << 16|45000); // manual mode,70% speed,45s
    rBASE_X    = (320 << 16)|320;
    rPIX_SENS  = (0x18 << 8)|0xa;
    rLPUSH_CFG = ((320 - 16) << 16)|(320 - 60);
    rRPUSH_CFG = ((320 + 16) << 16)|(320 + 60);
    rPUSH_CTRL = (0x1 << 24)|(0x20 << 16)|(0x5 << 8)|0x8; //pause mod3 ,view0, auto splice, push_en
    rAUX_CTRL  = 0x0;
 
    xil_printf("******************stag4 work start **********************************\n\r");
 
    while (1) {
        if (uistate.keypressed == 1){
           uistate.redraw = 2; 
           render();
        }
        else {
           udelay(1000);
           render();           
        }
    }
}
